package com.itcast.bulls.stock.position.service.impl;

import com.alibaba.druid.sql.ast.statement.SQLForeignKeyImpl;
import com.itcast.bulls.stock.common.math.MathUtils;
import com.itcast.bulls.stock.common.utils.GlobalConstants;
import com.itcast.bulls.stock.entity.quote.TradeStockQuoteLast;
import com.itcast.bulls.stock.entity.trade.TradePosition;
import com.itcast.bulls.stock.position.dao.trade.ITradePositionDao;
import com.itcast.bulls.stock.position.service.IStockPositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class StockPositionServiceImpl implements IStockPositionService {

    @Autowired
    private RedisCacheManager redisCacheManager;

    @Autowired
    private ITradePositionDao tradePositionDao;

    /**
     * 获取用户的所有持仓数据
     * @param accountId
     * @return
     */
    @Override
    public List<TradePosition> getAllPositions(long accountId) {
        List<TradePosition> positionList = tradePositionDao.getAllByAccountId(accountId);
        if(null != positionList){
            positionList.forEach(pos -> {
                //计算浮动盈亏
                calcProfit(pos);
            });
        }
        return positionList;
    }

    /**
     * 计算仓位的浮动盈亏
     * @param pos
     */
    private void calcProfit(TradePosition pos) {
        //1.获取最新的行情报价
        TradeStockQuoteLast quoteLast = getTradeStockQuoteLast(pos.getStockId());
        if(null != quoteLast){
            //浮动盈亏计算，采用常整型计算，保留两位小数
            //浮动盈亏计算： 浮动盈亏=（最新价-成本价）*持仓数量
            long diffPrice = MathUtils.sub(quoteLast.getLastPrice(), pos.getCostPrice());
            long profit = MathUtils.mul(diffPrice,pos.getVolume());
            pos.setProfit(profit);
            pos.setLastPrice(quoteLast.getLastPrice());
            //计算浮动盈亏的比例，采用常整型，保留四位小数（1699988 = 169.9988%）
            //盈亏比例=浮动盈亏/(成本价*持仓数量)
            pos.setProfitRate(MathUtils.div2Long(profit,MathUtils.mul(pos.getCostPrice(),pos.getVolume()),4));
        }
        //2.
    }

    //获取股票产品的最新报价
    private TradeStockQuoteLast getTradeStockQuoteLast(Long stockId) {
        Cache lastPriceCacqhe = redisCacheManager.getCache(GlobalConstants.STOCK_QUOTE_LAST_PRICE_KEY);
        Cache.ValueWrapper quoteLastWrapper = lastPriceCacqhe.get(stockId);
        TradeStockQuoteLast quoteLast = null;
        if(null != quoteLastWrapper){
            quoteLast = (TradeStockQuoteLast) quoteLastWrapper.get();
        }
        return quoteLast;
    }
}
